package com.hangzhou.xyj.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.model.ActionModel;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.widget.views.MyImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;

import java.util.List;

/**
 * Created by yaohuix on 2016/1/18.
 */
public class FindShowRecyclerAdapter extends BaseLoadingAdapter<ActionModel> {
    private final int SINGLE_MODE = 0x11;//单图模式
    private final int MUCH_MODE = 0x22;//多图模式

    public FindShowRecyclerAdapter(RecyclerView recyclerView, List<ActionModel> datas) {
        super(recyclerView, datas);
    }

    @Override
    public void onBindNormalViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case SINGLE_MODE:
                ((SingleImgViewHolder) holder).setData(mDataList.get(position));
                if (position == 0) {
                    ((SingleImgViewHolder) holder).vTopPadding.setVisibility(View.VISIBLE);
                }
                break;
            case MUCH_MODE:
            default:
                ((MuchImgViewHolder) holder).setData(mDataList.get(position));
                if (position == 0) {
                    ((MuchImgViewHolder) holder).vTopPadding.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = super.getItemViewType(position);
        if (viewType == TYPE_LOADING_ITEM) {//加载条目
            return viewType;
        } else {
            if (mDataList.get(position).getImgList().size() == 1) {
                return SINGLE_MODE;
            } else {
                return MUCH_MODE;
            }
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case SINGLE_MODE:
                return new SingleImgViewHolder(mLayoutInflater.inflate(R.layout.layout_find_show_item_single_img, parent, false));
            case MUCH_MODE:
            default:
                return new MuchImgViewHolder(mLayoutInflater.inflate(R.layout.layout_find_show_item_much_img, parent, false));
        }
    }

    class SingleImgViewHolder extends RecyclerView.ViewHolder {
        MyTextView name;//标题
        MyTextView content;//介绍
        MyTextView seat;//座位数
        MyTextView time;//时间
        MyTextView location;//地址
        MyImageView pic;//图片
        View vTopPadding;//上面的填充

        public SingleImgViewHolder(View view) {
            super(view);
            name = (MyTextView) view.findViewById(R.id.mtv_title);
            content = (MyTextView) view.findViewById(R.id.mtv_content);
            seat = (MyTextView) view.findViewById(R.id.mtv_seat);
            time = (MyTextView) view.findViewById(R.id.mtv_time);
            location = (MyTextView) view.findViewById(R.id.mtv_location);
            pic = (MyImageView) view.findViewById(R.id.miv_image);
            vTopPadding = view.findViewById(R.id.v_top_padding);
        }

        public void setData(ActionModel actionModel) {
            name.setText(actionModel.getName());
            content.setText(actionModel.getIntroduce());
            seat.setText("" + actionModel.getPartNum());
            time.setText(actionModel.getTime());
            location.setText(actionModel.getLocation());
            pic.displayImage(actionModel.getImgList().get(0));
        }
    }

    class MuchImgViewHolder extends RecyclerView.ViewHolder {
        MyTextView name;//标题
        MyTextView content;//介绍
        MyTextView seat;//座位数
        MyTextView time;//时间
        MyTextView location;//地址
        MyImageView pic1;//图片1
        MyImageView pic2;//图片2
        MyImageView pic3;//图片3
        View vTopPadding;

        public MuchImgViewHolder(View view) {
            super(view);
            name = (MyTextView) view.findViewById(R.id.mtv_title);
            content = (MyTextView) view.findViewById(R.id.mtv_introduce);
            seat = (MyTextView) view.findViewById(R.id.mtv_seat);
            time = (MyTextView) view.findViewById(R.id.mtv_time);
            location = (MyTextView) view.findViewById(R.id.mtv_location);
            pic1 = (MyImageView) view.findViewById(R.id.miv_image1);
            pic2 = (MyImageView) view.findViewById(R.id.miv_image2);
            pic3 = (MyImageView) view.findViewById(R.id.miv_image3);
            vTopPadding = view.findViewById(R.id.v_top_padding);
        }

        public void setData(ActionModel actionModel) {
            name.setText(actionModel.getName());
            content.setText(actionModel.getIntroduce());
            seat.setText("" + actionModel.getPartNum());
            time.setText(actionModel.getTime());
            location.setText(actionModel.getLocation());
            pic1.displayImage(actionModel.getImgList().get(0));
            pic2.displayImage(actionModel.getImgList().get(1));
            if (actionModel.getImgList().size() == 3) {
                pic3.displayImage(actionModel.getImgList().get(2));
            } else {
                pic3.setVisibility(View.GONE);
            }
        }
    }
}
